<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 12:01
 */

namespace plugin\admin\app\logic\plan;


use app\exception\IndexException;
use plugin\admin\app\exception\AdminException;
use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\novel\NovelPlanPromptTagsModel;
use plugin\admin\app\model\plan\PromotionPlanModel;
use support\Request;
use support\Response;

class PromptTagsLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new NovelPlanPromptTagsModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','created_at');
            $request->inputSet('order','desc');
        }

        $request->inputSet('isdel','0');
        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
        $planMap = $this->getListMapById(PromotionPlanModel::getInstance(),$items,'planid','id');
        foreach($items as &$val){
            $val['planname'] = isset($planMap[$val['planid']])?$planMap[$val['planid']]['name']:$val['planid'];
            //$val['select_tag'] = !empty($val['select_tag'])?json_decode($val['select_tag'], true):[];
        }

        if(request()->input('select')){

            $commonTags = array_filter(explode('|@@|',config('param.common_prompt_tags','')));
            $commonTagsDatas = [];
            foreach($commonTags as $tag){
                $tags = explode('||',$tag);
                if(!empty($tags[0]) && !empty($tags[1])){
                    $commonTagsDatas[] = [
                        'name'=>$tags[0],
                        'prompt'=>$tags[1],
                    ];
                }
            }
            $items = array_merge($items,$commonTagsDatas);
        }

        $data = [];
        if(request()->input('batch')){
            $prompttags = [];
            foreach($items as $v){
                $prompttags[] = $v['name'].'||'.$v['prompt'];
            }
            $prompttags = array_reverse($prompttags);
            $data[]['prompttags'] = implode("\n",$prompttags);

            $items = $data;
        }

        return parent::formatNormal($items, $total);
    }

    public function insert(Request $request): Response
    {

        return parent::insert($request);
    }

    public function update(Request $request): Response
    {
        return parent::update($request);
    }

    public function delete(Request $request): Response
    {
        return parent::isDelete($request);
    }

    public function batchInsert(Request $request): Response
    {
        $promptTags = $request->input('prompttags');
        $planid = $request->input('planid');
        if($planid==false){
            throw new IndexException(4020,'方案不存在');
        }
        $promptTags = array_filter(explode("\n",(string)$promptTags));

        $names = [];
        foreach($promptTags as $val){
            $row = explode('||',$val);
            if(!empty($row[0]) && !empty($row[1])){
                $first = $this->model->getWhere(['name'=>$row[0],'planid'=>$planid,'isdel'=>0])->first();
                if($first){
                    $first->prompt = $row[1];
                    $first->status = 1;
                    $first->save();
                }else{
                    $insert = [
                        'planid'=>$planid,
                        'name'=>$row[0],
                        'prompt'=>$row[1],
                    ];
                    $this->model->insertGetId($insert);
                }

                $names[] = $row[0];
            }
        }

        //这次去掉的记录给删掉
        $promptTagsNames = $this->model->getWhere(['planid'=>$planid,'isdel'=>0])->pluck('name')->toArray();
        $diffNames = array_diff($promptTagsNames,$names);
        if($diffNames){
            $this->model->getWhere([['name','in',$diffNames],'planid'=>$planid])->update(['isdel'=>time()]);
        }

        return $this->json(0,'ok',[]);
    }
}